﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl.Daily
{
    public partial class EMASppPlan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!Page.IsPostBack)
            {
                txt_date.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_date.Text = DateTime.Now.ToString("yyyy/MM/dd");                
                LoadDrop();
                ddl_Mtc.Items.Clear();
                if (Drop_BanZu.Items.Count != 0)
                {
                    DataSet ds = SelContractorByCId("日常设备维护");
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count != 0)
                    {
                        ddl_Mtc.DataSource = dt;
                        ddl_Mtc.DataTextField = "CName";
                        ddl_Mtc.DataValueField = "id";
                        ddl_Mtc.DataBind();
                    }
                }
                LoadPForm();
            }
        }
        protected void LoadDrop()
        {
            string userid = Session["PersonnelID"].ToString();
            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_ZuoYeQu, isAdmin, groupid, companyid, false);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_ZuoYeQu.SelectedItem, Drop_BanZu, isAdmin, classid, false);

            if (Session["RBGropuID"] != null) this.Drop_ZuoYeQu.SelectedValue = Session["RBGropuID"].ToString();
            if (Session["RBClassID"] != null) this.Drop_BanZu.SelectedValue = Session["RBClassID"].ToString();
            if (!isAdmin)
            {
                this.Drop_ZuoYeQu.Enabled = false;
                this.Drop_BanZu.Enabled = false;
            }

            Session.Remove("RBGropuID");
            Session.Remove("RBClassID");
            Session.Remove("RBDate");     
        }
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }
        protected void Drop_ZuoYeQu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");
            ddl_Mtc.Items.Clear();
            if (Drop_BanZu.Items.Count != 0)
            {
                DataSet ds = SelContractorByCId("日常设备维护");
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count != 0)
                {
                    ddl_Mtc.DataSource = dt;
                    ddl_Mtc.DataTextField = "CName";
                    ddl_Mtc.DataValueField = "id";
                    ddl_Mtc.DataBind();
                }
            }
            LoadPForm();
        }
        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddl_Mtc.Items.Clear();
            //if (Drop_BanZu.Items.Count != 0)
            //{
            //    DataSet ds = SelContractorByCId("日常设备维护");
            //    DataTable dt = ds.Tables[0];
            //    if (dt.Rows.Count != 0)
            //    {
            //        ddl_Mtc.DataSource = dt;
            //        ddl_Mtc.DataTextField = "CName";
            //        ddl_Mtc.DataValueField = "id";
            //        ddl_Mtc.DataBind();
            //    }
            //}
            LoadPForm();
        }
        /// <summary>
        /// 加载Rep
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="classname"></param>
        public void LoadRep()
        {            
            LoadPForm();
        }
        public DataSet SelContractorByCId(string contype)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,CName");
            strSql.Append(" FROM Contractor ");
            if (contype.Trim() != "")
            {
                strSql.Append(" where ContraType='" + contype + "'");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// 加载产品采购表
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="classname"></param>
        public void LoadPForm()
        {           
            try
            {
                DataTable dt_PPlan = new Catering_Rules.EMASppPlanP().GetList("ClassID ='" + Drop_BanZu.SelectedValue + "' and ContractorID='" + ddl_Mtc.SelectedValue + "'").Tables[0];
                dt_PPlan.Columns.Add("tempId");

                for (int i = 0; i < dt_PPlan.Rows.Count; i++)
                {
                    dt_PPlan.Rows[i]["tempId"] = i + 1;
                }
                this.Rep_PPlan.DataSource = dt_PPlan;
                this.Rep_PPlan.DataBind();
            }
            catch
            {

            }            
        }

       
        [WebMethod]
        public static string PAdd(string AccessName, string ModelRuler, string AccessUnit, string AccessNumber, string ConDesc, string Drop_ZuoYeQu, string Drop_BanZu, string ddl_Mtc, string txt_date, string hdn_pType_filepath)
        {
            Catering_Model.EMASppPlanP _emasppPlan = new Catering_Model.EMASppPlanP();
            _emasppPlan.DailyManagementID = int.Parse("1");
            _emasppPlan.GroupID = Drop_ZuoYeQu;
            _emasppPlan.ClassID = Drop_BanZu;
            _emasppPlan.AccessName = AccessName;
            _emasppPlan.ModelRuler = ModelRuler;
            _emasppPlan.AccessUnit = AccessUnit;
            _emasppPlan.AccessNumber = AccessNumber;
            _emasppPlan.ConDesc = ConDesc;
            _emasppPlan.ContractorID = ddl_Mtc;
            _emasppPlan.AddDate = Convert.ToDateTime(txt_date);
            if (!string.IsNullOrEmpty(hdn_pType_filepath) && hdn_pType_filepath.Length > 1)
            {
                _emasppPlan.wendang = hdn_pType_filepath;
            } 
            try
            {
                new Catering_Rules.EMASppPlanP().Add(_emasppPlan);
            }
            catch
            {
                return "FALSE";
            }
            
            return "OK";
        }

        //修改
        [WebMethod]
        public static string PEdit(string _id, string AccessName, string ModelRuler, string AccessUnit, string AccessNumber, string ConDesc, string hdn_pType_filepath)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.EMASppPlanP _EPP = new Catering_Model.EMASppPlanP();

            _EPP.Id = id;
            _EPP.AccessName = AccessName;
            _EPP.ModelRuler = ModelRuler;
            _EPP.AccessUnit = AccessUnit;
            _EPP.AccessNumber = AccessNumber;
            _EPP.ConDesc = ConDesc;            
            if (!string.IsNullOrEmpty(hdn_pType_filepath) && hdn_pType_filepath.Length > 1)
            {
                _EPP.wendang = hdn_pType_filepath;
            } 
            try
            {
                new Catering_Rules.EMASppPlanP().Update(_EPP);
            }
            catch
            {
                return "FALSE";
            }
            return "OK";
        }
       
        [WebMethod]
        public static ArrayList PGet(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.EMASppPlanP md = new Catering_Rules.EMASppPlanP().GetModel(Convert.ToInt32(_id));            
            al.Add(md.Id);
            al.Add(md.AccessName);
            al.Add(md.ModelRuler);
            al.Add(md.AccessUnit);
            al.Add(md.AccessNumber);
            al.Add(md.ConDesc);
            al.Add(md.wendang);
            return al;
        }
       
        protected void Rep_PPlan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Pdel")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.EMASppPlanP().Delete(delID))
                {
                    this.LoadPForm();
                }
            }
        }

        protected void ddl_Mtc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPForm();
        }
        #region 转换文件
        public string formatfilename(string filenames)
        {
            if (!string.IsNullOrEmpty(filenames))
            {
                var files = filenames.Split(',');
                filenames = "";
                int num = 0;
                foreach (var item in files)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        ++num;
                        filenames += string.Format("<a style='text-decoration:underline' href='../../../UpFile/{0}'>{1}</a>&nbsp;", item, "型号说明书" + num);
                    }
                }
            }
            return filenames;
        }
    }
        #endregion
}